<?php
	if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Author_Ctrl extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
		}
		
		function index($query_id=0, $order_by  = 'created_date', $sort_order = 'asc', $current_page = 0){
			$search_by='';
			$search_by_value='';
			$data['search_by'] = '';
			$data['search_by_value'] = '';
			//echo '<pre>';print_r($_SESSION);exit;
				if($query_id == '0'){		
				//echo '<pre>';print_r($_SESSION);exit;
					unset($_SESSION['search']);
				}else{
					if(isset($_SESSION['search'])){
					$array = $_SESSION['search'];
					//echo '<pre>';print_r($_SESSION);exit;
					$search_by = $array['search_by'];
					$search_by_value = $array['search'];
					
					$data['search_by'] = $search_by;
					$data['search_by_value'] = $search_by_value;
					}
				}
			
			$results_per_page = 4;
			
			$url = 'http://localhost/azindia/azindia-api/en/json.api';
			$data = array('www-command'=>'articles-get_authors','current_page'=>$current_page,'results_per_page'=>$results_per_page,'order_by'=>$order_by,'sort_order'=>$sort_order,'search_by'=>$search_by,'search_by_value'=>$search_by_value);
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response = json_decode(curl_exec($ch));
			//echo '<pre>';print_r($response);exit;
			curl_close($ch);
			if($response->message !== 'No Authors'){
			$this->load->library('pagination');
			$config['base_url'] = 'http://localhost/azindia/azindia-client/admin/author_ctrl/index/'.$query_id.'/'.$order_by.'/'.$sort_order;
			$config['per_page'] = $results_per_page;
			$config['total_rows']  = $response->total;
			$config['uri_segment'] = 7;
			$config['num_links'] = 1;
			$config['prev_link'] = 'Previous';
			$config['next_link'] = 'Next';
			$config['first_link'] = 'First';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();			
			$data['articles_get_authors'] = $response;
			$data['error'] = '';
			}else{
				$data['error'] = 1;
				$data['articles_get_authors'] = '';
			}
			//echo '<pre>';print_r($data);exit;
			$data['author_fileds'] = array('author_name'=>'Author Name', 'email'=>'Email', 'status'=>'Status');
			$data['order_by'] = $order_by;
			$data['sort_order'] = $sort_order;
			$data['query_id'] = $query_id;
			$data['status'] = array(1=>'Active', 0=>'In-active');
			
			$this->load->view('admin/author_view',$data);
		}
		
		function search_by(){
			
		
		if($this->input->post('search') == ''){
			$search = $this->input->post('search1');
		}else{
			$search = $this->input->post('search');
		}
			
		$query_array = array(
			'search_by'=>$this->input->post('search_by'),
			'search'=>$search
		);
		$query_id=rand();
		$_SESSION['search']=$query_array;
		//echo '<pre>';print_r($_SESSION['search']);exit;
		redirect('admin/author_ctrl/index/'.$query_id);
	}
		
		function create_author_page(){
			$this->load->view('admin/authors_form_view');
		}
			
			function create_author(){
				
			$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('title', 'Title', 'required|alpha|min_length[3]|max_length[50]');
				$this->form_validation->set_rules('first_name', 'Firstname', 'required|alpha|min_length[3]|max_length[50]');
				$this->form_validation->set_rules('middle_name', 'Middlename', 'required|alpha|min_length[3]|max_length[50]');
				$this->form_validation->set_rules('last_name', 'Lastname', 'required|alpha|min_length[3]|max_length[50]');
				$this->form_validation->set_rules('address1', 'Address1', 'min_length[10]|max_length[50]');
				$this->form_validation->set_rules('address2', 'Address2', 'min_length[10]|max_length[50]');
				$this->form_validation->set_rules('city', 'city', 'alpha');
				$this->form_validation->set_rules('state', 'State', 'alpha');
				$this->form_validation->set_rules('country', 'Country', 'alpha');
				$this->form_validation->set_rules('zip', 'Zip', 'min_length[5]|numeric');
				$this->form_validation->set_rules('phone1', 'Phone1', 'regex_match[/^[0-9]{10}$/]');
				$this->form_validation->set_rules('phone2', 'Phone2', 'regex_match[/^[0-9]{10}$/]');
				$this->form_validation->set_rules('email', 'Email', 'valid_email');
				$this->form_validation->set_rules('note', 'note', 'rmin_length[12]|max_length[100]');
				
				if($this->form_validation->run() == FALSE){
						$this->load->view('admin/authors_form_view');
				}else{
					$url = 'http://localhost/azindia/azindia-api/json.api';
					$data = array('www-command'=>'articles-create_author', 
					'author_title' => $this->input->post('title'),
				'first_name' => $this->input->post('first_name'),
				'middle_name' => $this->input->post('middle_name'),
				'last_name' => $this->input->post('last_name'),
				'address1' => $this->input->post('address1'),
				'address2' => $this->input->post('address2'),
				'city' => $this->input->post('city'),
				'state' => $this->input->post('state'),
				'country' => $this->input->post('country'),
				'zip' => $this->input->post('zip'),
				'phone1' => $this->input->post('phone1'),
				'phone2' => $this->input->post('phone2'),
				'email' => $this->input->post('email'),
				'description' => $this->input->post('note'),
				'created_by' => 1
				);
				//echo '<pre>';print_r($data);exit;
				$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//$data['articles_create_author'] = $response;
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/author_ctrl/index/');
				}
			}
			
				function edit_author_form(){
				$url = 'http://localhost/azindia/azindia-api/json.api';
				if($this->uri->segment(4) > 0){
					$_SESSION['author_id']=$this->uri->segment(4);
					//echo 'hi';exit;
				}
				$data = array('www-command'=>'articles-get_author','author_id'=>$_SESSION['author_id']);
				//echo '<pre>';print_r($data);exit;
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				$response = (array) $response;
				foreach($response as $value){
					$value = $value;
				}
				//echo '<pre>';print_r($value);exit;
				$data['author_id'] = $value->author_id;
				$data['title'] = $value->title;
				$data['first_name'] = $value->first_name;
				$data['last_name'] = $value->last_name;
				$data['middle_name'] = $value->middle_name;
				$data['address1'] = $value->address1;
				$data['address2'] = $value->address2;
				$data['city'] = $value->city;
				$data['state'] = $value->state;
				$data['country'] = $value->country;
				$data['zip'] = $value->zip;
				$data['phone1'] = $value->phone1;
				$data['phone2'] = $value->phone2;
				$data['email'] = $value->email;
				$data['description'] = $value->description;
				$data['created_by'] = $value->created_by;
				$this->load->view('admin/edit_author_form',$data);
		}
	
		function edit_author(){
			$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('title', 'Title', 'required|alpha|min_length[3]|max_length[50]');
				$this->form_validation->set_rules('first_name', 'Firstname', 'required|alpha|min_length[3]|max_length[50]');
				$this->form_validation->set_rules('middle_name', 'Middlename', 'required|alpha|min_length[3]|max_length[50]');
				$this->form_validation->set_rules('last_name', 'Lastname', 'required|alpha|min_length[3]|max_length[50]');
				$this->form_validation->set_rules('address1', 'Address1', 'min_length[10]|max_length[50]');
				$this->form_validation->set_rules('address2', 'Address2', 'min_length[10]|max_length[50]');
				$this->form_validation->set_rules('city', 'city', 'alpha');
				$this->form_validation->set_rules('state', 'State', 'alpha');
				$this->form_validation->set_rules('country', 'Country', 'alpha');
				$this->form_validation->set_rules('zip', 'Zip', 'min_length[5]|numeric');
				$this->form_validation->set_rules('phone1', 'Phone1', 'regex_match[/^[0-9]{10}$/]');
				$this->form_validation->set_rules('phone2', 'Phone2', 'regex_match[/^[0-9]{10}$/]');
				$this->form_validation->set_rules('email', 'Email', 'valid_email');
				$this->form_validation->set_rules('note', 'note', 'rmin_length[12]|max_length[100]');
				
				if($this->form_validation->run() == FALSE){
					//echo 'hi';exit;
						$data['author_id'] = $this->input->post('hidden_id');
						$data['title'] = $this->input->post('title');
						$data['first_name'] = $this->input->post('first_name');
						$data['last_name'] = $this->input->post('last_name');
						$data['middle_name'] = $this->input->post('middle_name');
						$data['address1'] = $this->input->post('address1');
						$data['address2'] = $this->input->post('address2');
						$data['city'] = $this->input->post('city');
						$data['state'] = $this->input->post('state');
						$data['country'] = $this->input->post('country');
						$data['zip'] = $this->input->post('zip');
						$data['phone1'] = $this->input->post('phone1');
						$data['phone2'] = $this->input->post('phone2');
						$data['email'] = $this->input->post('email');
						$data['description'] = $this->input->post('note');
						$data['created_by'] = $this->input->post('created_by');
						//echo '<pre>';print_r($data);exit;
						$this->load->view('admin/edit_author_form',$data);
				}else{
					$url = 'http://localhost/azindia/azindia-api/json.api';
					$data = array('www-command'=>'articles-update_author',
					'author_id' => $this->input->post('hidden_id'), 
					'title' => $this->input->post('title'),
					'first_name' => $this->input->post('first_name'),
					'middle_name' => $this->input->post('middle_name'),
					'last_name' => $this->input->post('last_name'),
					'address1' => $this->input->post('address1'),
					'address2' => $this->input->post('address2'),
					'city' => $this->input->post('city'),
					'state' => $this->input->post('state'),
					'country' => $this->input->post('country'),
					'zip' => $this->input->post('zip'),
					'phone1' => $this->input->post('phone1'),
					'phone2' => $this->input->post('phone2'),
					'email' => $this->input->post('email'),
					'description' => $this->input->post('note'),
					'created_by' => $this->input->post('created_by')
					);
				//echo '<pre>';print_r($data);exit;
				$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/author_ctrl/index/');
				}
		}
		
		
		function delete_author(){
			$url = 'http://localhost/azindia/azindia-api/json.api';
				if($this->uri->segment(5) == 0){
					$status = 1;
				}else{
					$status = 0;
				}
				$data = array('www-command'=>'articles-delete_author','status'=>$status,'author_id'=>$this->uri->segment(4));
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				
				redirect('admin/author_ctrl/index/');

		}
		
	}
?>